package com.zhanggm.student.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhanggm.student.entity.Area;
import com.zhanggm.student.entity.Student;
import com.zhanggm.student.mapper.StudentMapper;
import com.zhanggm.student.model.StudentModel;
import com.zhanggm.student.service.IAreaService;
import com.zhanggm.student.service.IHobbyService;
import com.zhanggm.student.service.IStudentHobbyRelationService;
import com.zhanggm.student.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 学生表 服务实现类
 * </p>
 *
 * @author zhanggm
 * @since 2020-10-21
 */
@Slf4j
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Autowired
    private IStudentHobbyRelationService iStudentHobbyRelationService;

    @Autowired
    private IAreaService iAreaService;

    @Autowired
    private IHobbyService iHobbyService;


    @Autowired
    private ThreadPoolTaskExecutor poolTaskExecutor;

//    @PostConstruct
    public void thread() {
//        MyThread myThread = new MyThread();
        poolTaskExecutor.execute(()->{
            while (true) {
                String name = Thread.currentThread().getName();
                log.info("线程运行中:{}",name);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public PageInfo<StudentModel> getPageInfo(StudentModel studentModel, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<StudentModel> students = getBaseMapper().selectPage(studentModel);
        //设置省市区的name值
        students.forEach(stu->{
            Area provice = iAreaService.getById(stu.getProviceId());
            stu.setProviceName(provice.getName());
            Area city = iAreaService.getById(stu.getCityId());
            stu.setCityName(city.getName());
            Area area = iAreaService.getById(stu.getAreaId());
            stu.setAreaName(area.getName());
            //设置爱好
            stu.setHobbyNames(iHobbyService.getNames(stu.getId()));
        });

        return new PageInfo<StudentModel>(students);
    }

    @Override
    public List<Integer> getHobbyIdListByStuId(Integer stuId) {
        return iStudentHobbyRelationService.getHobbyIdListByStuId(stuId);
    }
}
